/*
 * Created on 2005/05/16
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.ui.draw2d;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.swt.graphics.Color;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * 
 *
 */
public class WaveFormFigure extends DoubleScalableFigure implements ChainedFigure{
    public static Log log = LogFactory.getLog(WaveFormFigure.class);
    public static final int STYLE_GRAPH=1;
    public static final int STYLE_BAR=0;
    private int style; //TODO add bar mode,get min and max and draw
    
    private int pixelHeight;
    WaveFormFigure prevFigure;
    byte[] bytes;
 
    /* (non-Javadoc)
     * @see org.jpn.xucker.rcp.draw2d.ChainedFigure#setFigure(org.jpn.xucker.rcp.draw2d.ChainedFigure)
     */
    
    public byte getByteAt(int index){
        return bytes[index];
    }
    private boolean drawPrev=true;
    public WaveFormFigure(byte[] bytes,int pixelHeight){
        super(bytes.length,pixelHeight);
        this.bytes=bytes;
        this.pixelHeight=pixelHeight;
        resize();
        
        
    }
    public void setFigure(ChainedFigure figure) {
        // TODO Auto-generated method stub
        prevFigure=(WaveFormFigure)figure;
    }
    public int getLength(){
        return bytes.length;
    }
    public void paintFigure(Graphics g) {
       // log.infopaint:"+bytes.length);
        int dx;
        int centerY=(int)(pixelHeight*hzoom/2);
        int dy;
        
        int prey=0;
        int prex=0;
        //g.setBackgroundColor(ColorConstants.gray);
        //g.fillRectangle(getClientArea());
        
        g.setForegroundColor(getForegroundColor());
        //log.info(getBackgroundColor());
        //log.info(getForegroundColor());
        //log.infodraw:"+wzoom);
        for(int i=0;i<bytes.length;i++){
            
            dx=getClientArea().x+(int)(i*wzoom);
            dy=getClientArea().y+centerY+(int)(((int)bytes[i])*hzoom);
            
            if(i==0){
            if(prevFigure!=null){
            prex=dx-Math.max(1,(int)wzoom);
            prey=centerY+(int)(prevFigure.getLastByte()*hzoom);
            //log.infopre:"+prex+","+prey);
            }else{
                prex=dx;
                prey=dy;
            }
            
            //if(i==0){
               // log.warn("paint: "+prex+":"+prey+","+dx+","+dy);
            //}
            //log.infoin:"+this.getInsets());
           // log.infoborder:"+this.getBorder());
            //log.info(getBounds()+","+getClientArea());
            //log.infopaint: "+prex+":"+prey+","+dx+","+dy);
            }
            if(prex!=dx || prey!=dy){
            g.drawLine(prex,prey,dx,dy);
            }
            prex=dx;
            prey=dy;
           
        }
        
        }
    public int length(){
        return bytes.length;
    }
    public byte getLastByte(){
        return bytes[bytes.length-1];
    }

}
